package handler;

import handler.server.*;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.AdaptiveRecvByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;

/**
 * @author dfj@99chuxing.com
 * @date 2017/12/13 0013 17:21
 */
public class ServerStart {

    // 读超时
    private static final int READ_IDEL_TIME_OUT = 10;
    // 写超时
    private static final int WRITE_IDEL_TIME_OUT = 10;
    // 所有超时
    private static final int ALL_IDEL_TIME_OUT = 15;






    public static  void  main(String[] args){


        /** 创建引导器*/
        ServerBootstrap serverBootstrap =new ServerBootstrap();
        /** 创建事件循环监听器*/
        NioEventLoopGroup eventLoopGroup =new NioEventLoopGroup();
        try {
            serverBootstrap
                    // 添加事件循环监听
                    .group(eventLoopGroup)
                    //  使用的通道
                    .channel(NioServerSocketChannel.class)
                    //绑定的端口
                    .localAddress(new InetSocketAddress(2345))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            /** 初始化channel时添加handler到该channel的pipeline*/
                            socketChannel.pipeline().addLast(new ConnectHandler())
                                    .addLast("Encoder",new ServerEncoder())
                                    .addLast("Decoder",new ServerDecoder())
                                    .addLast(new ServerHandler())
                                    /** 服务端添加IdleStateHandler心跳检测处理器*/
                                    .addLast("HeartBeat",new HeartBeatServerHandler())
                                    .addLast(new TetsHandler())
                                    .addLast(new IdleStateHandler(READ_IDEL_TIME_OUT,WRITE_IDEL_TIME_OUT,ALL_IDEL_TIME_OUT, TimeUnit.SECONDS));

                        }
                    }).option(ChannelOption.TCP_NODELAY,true)
                      .option(ChannelOption.RCVBUF_ALLOCATOR,new AdaptiveRecvByteBufAllocator(128,1024,1024));

            // sync()方法的调用将导致当前 Thread阻塞，一直到绑定操作完成为止
            ChannelFuture future =serverBootstrap.bind().sync();
            future.addListener(new GenericFutureListener<Future<? super Void>>() {
                @Override
                public void operationComplete(Future<? super Void> future) throws Exception {
                    if (future.isSuccess()){
                        System.out.println("端口绑定成功-------------");
                    }
                }
            });
            //将会阻塞等待直到服务器的 Channel关闭（因为你在 Channel 的 CloseFuture 上调用了 sync()方法）
            future.channel().closeFuture().sync();
            System.out.println("服务器通道关闭-------");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            eventLoopGroup.shutdownGracefully();
        }



    }

}
